package com.bigfire.dtm.starter.datasource;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Optional;

/**
 * 汉德森结算中心数据库链接配置
 * @author Damon S.
 * @version v1.0.1
 * @date 2019年11月03日 20:52
 */
//@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "acSettlementEntityManagerFactory",
        transactionManagerRef = "acSettlementTransactionManager",
        basePackages = {"com.hanwin.settlement.dal.entity.anchnet"})
@RequiredArgsConstructor
@Profile("realtime")
public class AcSettlementDataConfig {

    private final HibernateProperties hibernateProperties;
    private final JpaProperties jpaProperties;


    @Bean(name = "acSettlementDataSource")
    @Qualifier("acSettlementDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.acsettlement")
    public DataSource acSettlementDataSource() {
        return DataSourceBuilder.create().build();
    }



    @Bean(name = "acSettlementEntityManager")
    public EntityManager acSettlementEntityManager(EntityManagerFactoryBuilder builder) {
        EntityManagerFactory factory =
                Optional.ofNullable(acSettlementEntityManagerFactory(builder))
                        .orElseThrow(NullPointerException::new)
                        .getObject();
        return Optional.ofNullable(factory).orElseThrow(NullPointerException::new).createEntityManager();
    }


    @Bean(name = "acSettlementEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean acSettlementEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(acSettlementDataSource())
                //设置实体类所在位置.扫描所有带有 @Entity 注解的类
                .packages("com.hanwin.settlement.dal.entity.anchnet")
                // Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
                // Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作
                .persistenceUnit("acSettlementPersistenceUnit")
                .properties(hibernateProperties.determineHibernateProperties(
                        jpaProperties.getProperties(), new HibernateSettings()))
                .build();
    }


    @Bean(name = "acSettlementTransactionManager")
    public PlatformTransactionManager acSettlementTransactionManager(EntityManagerFactoryBuilder builder) {
        EntityManagerFactory factory =
                Optional.ofNullable(acSettlementEntityManagerFactory(builder))
                        .orElseThrow(NullPointerException::new)
                        .getObject();
        return new JpaTransactionManager(
                Optional.ofNullable(factory).orElseThrow(NullPointerException::new));
    }
}
